home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / widgets-mesa / src / MesaDrawingArea.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-31  |  8.4 KB  |  275 lines

  1. /* MesaDrawingArea.c -- Implementation file for the Mesa widget
  2.    Copyright (C) 1995, 1996 Thorsten.Ohl @ Physik.TH-Darmstadt.de
  3.  
  4.    This library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Library General Public
  6.    License as published by the Free Software Foundation; either
  7.    version 2 of the License, or (at your option) any later version.
  8.  
  9.    This library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU Library General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU Library General Public
  15.    License along with this library; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.    $Id: MesaDrawingArea.c,v 1.20 1996/09/30 00:21:07 ohl Exp $
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <X11/IntrinsicP.h>
  24. #include <X11/StringDefs.h>
  25. #include <X11/Shell.h>
  26. #ifdef __GLX_MOTIF
  27. #include <GL/MesaMDrawingAreaP.h>
  28. #else
  29. #include <GL/MesaDrawingAreaP.h>
  30. #endif
  31. #ifndef GLwDebug
  32. #define GLwDebug(w) 0
  33. #endif
  34.  
  35. #ifdef __GLX_MOTIF
  36. #define GLwDrawingAreaWidget       GLwMDrawingAreaWidget
  37. #define GLwDrawingAreaClassRec     GLwMDrawingAreaClassRec
  38. #define GLwDrawingAreaRec          GLwMDrawingAreaRec
  39. #define glwDrawingAreaClassRec     glwMDrawingAreaClassRec
  40. #define glwDrawingAreaWidgetClass  glwMDrawingAreaWidgetClass
  41. #define MesaDrawingAreaWidget      MesaMDrawingAreaWidget
  42. #define MesaDrawingAreaClassRec    MesaMDrawingAreaClassRec
  43. #define MesaDrawingAreaRec         MesaMDrawingAreaRec
  44. #define mesaDrawingAreaClassRec    mesaMDrawingAreaClassRec
  45. #define mesaDrawingAreaWidgetClass mesaMDrawingAreaWidgetClass
  46. #endif
  47.  
  48.  
  49. /* Resources. */
  50.  
  51. #define offset(_field)  XtOffsetOf(MesaDrawingAreaRec, mesaDrawingArea._field)
  52. static XtResource resources[] =
  53. {
  54.   /* name, class, type, size,
  55.      offset, default_type, default_addr */
  56.   {GLwNximage, GLwCXImage, XtRBoolean, sizeof (Boolean),
  57.    offset (ximage), XtRBoolean, (caddr_t) False},
  58.   {GLwNshareLists, GLwCShareLists, XtRBoolean, sizeof (Boolean),
  59.    offset (share_lists), XtRBoolean, (caddr_t) False},
  60.   {GLwNshareListsWith, GLwCShareListsWith, XtRWidget, sizeof (Widget),
  61.    offset (share_lists_with), XtRWidget, NULL},
  62. };
  63. #undef offset
  64.  
  65. /* Basic widget methods.  */
  66.  
  67. /* Initialize this widget class.  Currently we just clear the pointer
  68.    to the widget we're sharing lists with.  */
  69.  
  70. static void
  71. ClassInitialize (void)
  72. {
  73.   MesaListsRoot = NULL;
  74. }
  75.  
  76. /* Initialize a widget instance.  */
  77.  
  78. static void
  79. Initialize (Widget request, Widget new, ArgList args, Cardinal * num_args)
  80. {
  81.   LOG (new);
  82. }
  83.  
  84. static void
  85. Realize (Widget w, XtValueMask *mask,
  86.      XSetWindowAttributes *attr)
  87. {
  88.   XMesaContext share_lists_with;
  89.   
  90.   LOG (w);
  91.  
  92.   /* Trying to outsmart ourselves with the cute looking
  93.      `(XtSuperclass (w)->core_class.realize) (w, mask, attr);'
  94.      is a BAD idea, because it will lead to infinite recursion if a
  95.      subclass has the same idea ...  */
  96.  
  97.   (glwDrawingAreaClassRec.core_class.realize) (w, mask, attr);
  98.  
  99.   if (MesaShareLists (w) && (MesaListsRoot != NULL))
  100.     {
  101.       if (GLwDebug(w))
  102.     fprintf (stderr, "sharing lists with %p...\n", MesaListsRoot);
  103.       share_lists_with = MesaContext (MesaListsRoot);
  104.     }
  105.   else
  106.     {
  107.       if (GLwDebug(w))
  108.     fprintf (stderr, "not sharing lists...\n");
  109.       share_lists_with = NULL;
  110.     }
  111.  
  112.   /* Attach a Mesa rendering context to the window */
  113.   MesaVisual (w) =
  114.     XMesaCreateVisual (XtDisplay (w),
  115.                ((MesaDrawingAreaWidget)w)->glwDrawingArea.visualInfo,
  116.                (GLboolean) MesaRGBA (w),
  117.                (MesaAlphaSize(w) > 0) ? GL_TRUE : GL_FALSE,
  118.                (GLboolean) MesaDoublebuffer (w),
  119.                (GLboolean) MesaXImage (w),
  120.                MesaDepthSize(w),
  121.                MesaStencilSize(w),
  122.                MesaAccumRedSize(w),
  123.                (GLint) 0 /* level */);
  124.   if (!MesaVisual (w))
  125.     {
  126.       printf ("Couldn't create Mesa/X visual!\n");
  127.       exit (1);
  128.     }
  129.  
  130.   MesaContext (w) = XMesaCreateContext (MesaVisual (w), share_lists_with);
  131.   if (!MesaContext (w))
  132.     {
  133.       printf ("Couldn't create Mesa/X context!\n");
  134.       exit (1);
  135.     }
  136.   if (MesaShareLists (w) && (MesaListsRoot == NULL))
  137.     {
  138.       if (GLwDebug(w))
  139.     fprintf (stderr, "installing %p as MesaListsRoot...\n", w);
  140.       MesaListsRoot = w;
  141.     }
  142.  
  143. #ifdef AVOID_MESABUFFER_INTERFACE
  144.   glXMakeCurrent (XtDisplay (w), XtWindow (w), MesaContext (w));
  145. #else
  146.   MesaBuffer (w) = XMesaCreateWindowBuffer (MesaVisual (w), XtWindow (w));
  147.   if (!MesaBuffer (w))
  148.     {
  149.       printf ("Couldn't create Mesa/X buffer!\n");
  150.       exit (1);
  151.     }
  152.  
  153.   XMesaMakeCurrent (MesaContext (w), MesaBuffer (w));
  154. #endif
  155.   if (GLwDebug(w))
  156.     fprintf (stderr, "finished with Realize() of %p.\n", w);
  157. }
  158.  
  159. static void
  160. Destroy (Widget w)
  161. {
  162.   LOG (w);
  163. #ifdef AVOID_MESABUFFER_INTERFACE
  164.   XMesaDestroyContext (MesaContext (w));
  165.   MesaContext (w) = NULL;
  166.   XMesaDestroyVisual (MesaVisual (w));
  167.   MesaVisual (w) = NULL;
  168. #else
  169.   XMesaDestroyBuffer (MesaBuffer (w));
  170.   MesaBuffer (w) = NULL;
  171.   XMesaDestroyContext (MesaContext (w));
  172.   MesaContext (w) = NULL;
  173.   XMesaDestroyVisual (MesaVisual (w));
  174.   MesaVisual (w) = NULL;
  175. #endif
  176. }
  177.  
  178. /* Resizing the widget can be done by adjusting the viewport.
  179.    However, we must make sure that the correct XMesaContext is
  180.    active.  It is also just common courtesy for an event handler
  181.    to restore the old XMesaContext afterwards.   */
  182.  
  183. static void
  184. Resize (Widget w)
  185. {
  186.   LOG (w);
  187.  
  188.   /* Ignore while there is no Mesa context.  */
  189.   if (XtIsRealized (w) && MesaContext (w))
  190.     {
  191. #ifdef AVOID_MESABUFFER_INTERFACE
  192.       GLXDrawable drawable = glXGetCurrentDrawable ();
  193.       GLXContext context = glXGetCurrentContext ();
  194.       glXMakeCurrent (XtDisplay (w), XtWindow (w), MesaContext (w));
  195.       glViewport (0, 0, w->core.width, w->core.height);
  196.       glXMakeCurrent (XtDisplay (w), drawable, context);
  197. #else
  198.       XMesaBuffer buffer = XMesaGetCurrentBuffer ();
  199.       XMesaContext context = XMesaGetCurrentContext ();
  200.       XMesaMakeCurrent (MesaContext (w), MesaBuffer (w));
  201.       glViewport (0, 0, w->core.width, w->core.height);
  202.       XMesaMakeCurrent (context, buffer);
  203. #endif
  204.     }
  205.  
  206.   /* Call our superclass' Resize method.  */
  207.   (glwDrawingAreaClassRec.core_class.resize) (w);
  208. }
  209.  
  210.  
  211. /* Now use all these methods in the widget class record.  */
  212.  
  213. MesaDrawingAreaClassRec mesaDrawingAreaClassRec =
  214. {
  215.   {
  216.     /* superclass            */ (WidgetClass) &glwDrawingAreaClassRec,
  217. #ifdef __GLX_MOTIF
  218.     /* class_name            */ "MesaMDrawingArea",
  219. #else
  220.     /* class_name            */ "MesaDrawingArea",
  221. #endif
  222.     /* widget_size           */ sizeof (MesaDrawingAreaRec),
  223.     /* class_initialize      */ ClassInitialize,
  224.     /* class_part_initialize */ NULL,
  225.     /* class_inited          */ FALSE,
  226.     /* initialize            */ Initialize,
  227.     /* initialize_hook       */ NULL,
  228.     /* realize               */ Realize,
  229.     /* actions               */ NULL,
  230.     /* num_actions           */ 0,
  231.     /* resources             */ resources,
  232.     /* num_resources         */ XtNumber (resources),
  233.     /* xrm_class             */ NULLQUARK,
  234.     /* compress_motion       */ TRUE,
  235.     /* compress_exposure     */ TRUE,
  236.     /* compress_enterleave   */ TRUE,
  237.     /* visible_interest      */ FALSE,
  238.     /* destroy               */ Destroy,
  239.     /* resize                */ (XtWidgetProc) Resize,
  240.     /* expose                */ XtInheritExpose,
  241.     /* set_values            */ NULL,
  242.     /* set_values_hook       */ NULL,
  243.     /* set_values_almost     */ XtInheritSetValuesAlmost,
  244.     /* get_values_hook       */ NULL,
  245.     /* accept_focus          */ NULL,
  246.     /* version               */ XtVersion,
  247.     /* callback_private      */ NULL,
  248.     /* tm_table              */ NULL,
  249.     /* query_geometry        */ XtInheritQueryGeometry,
  250.     /* display_accelerator   */ XtInheritDisplayAccelerator,
  251.     /* extension             */ NULL
  252.   },
  253. #ifdef __GLX_MOTIF
  254.   { /* XmPrimitive fields */
  255.     /* border_highlight      */ (XtWidgetProc) _XtInherit,
  256.     /* border_unhighlight    */ (XtWidgetProc) _XtInherit,
  257.     /* translations          */ XtInheritTranslations,
  258.     /* arm_and_activate      */ NULL,
  259.     /* get_resources         */ NULL,
  260.     /* num get_resources     */ 0,
  261.     /* extension             */ NULL,
  262.   },
  263. #endif /* __GLX_MOTIF */
  264.   { /* MesaDrawingArea fields*/
  265.     /* RCS_id                */
  266.     "@(#) $Id: MesaDrawingArea.c,v 1.20 1996/09/30 00:21:07 ohl Exp $"
  267.   }
  268. };
  269.  
  270. WidgetClass mesaDrawingAreaWidgetClass
  271.   = (WidgetClass) & mesaDrawingAreaClassRec;
  272.  
  273. /* The End. */
  274.  
  275.